package com.xzj.admin.boot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xzj.admin.boot.domain.entity.SysDept;
import com.xzj.admin.boot.domain.entity.SysRoleMenu;
import com.xzj.admin.boot.domain.entity.SysUserRole;
import com.xzj.admin.boot.domain.vo.SysDeptVO;
import com.xzj.admin.boot.domain.vo.SysRoleVO;
import com.xzj.admin.boot.domain.vo.SysUserVO;
import com.xzj.admin.boot.mapper.SysDeptMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.datasource.utils.XzjPageUtil;
import com.xzj.admin.boot.domain.entity.SysUser;
import com.xzj.admin.boot.mapper.SysUserMapper;
import com.xzj.admin.boot.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: SysUserServiceImpl
 * @description: 用户表;(sys_user)表服务实现类
 * @author: XiaZunJun
 * @create: 2024-7-30
 */
@Service
public class SysUserServiceImpl implements SysUserService{
    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    public SysUser queryById(String userId){
        return sysUserMapper.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    public PageResp<SysUserVO> pageQuery(PageReq<SysUserVO> pageReq){
        if(StrUtil.isNotEmpty(pageReq.getT().getDept())){
            String deptId = pageReq.getT().getDept();
            // 根据DeptId去获取所有下级或下下级的部门Id
            List<SysDeptVO> sysDeptVOS = sysDeptMapper.queryDeptList(new SysDept());
            List<String> deptIds = new ArrayList<>();
            judgeDeptId(deptIds, deptId, sysDeptVOS);
            pageReq.getT().setDeptIds(deptIds);
        }
        return XzjPageUtil.queryPage(pageReq, sysUser -> sysUserMapper.pageQuery(sysUser));
    }

    private void judgeDeptId(List<String> deptIds, String deptId, List<SysDeptVO> sysDeptVOS) {
        deptIds.add(deptId);
        for(SysDeptVO sysDeptVO : sysDeptVOS){
            if(deptId.equals(sysDeptVO.getParentDept())){
                deptIds.add(sysDeptVO.getDeptId());
                judgeDeptId(deptIds, sysDeptVO.getDeptId(), sysDeptVOS);
            }
        }
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    public SysUserVO insert(SysUserVO sysUser){
        sysUserMapper.insert(sysUser);
        roleMenuHandle(sysUser);
        return sysUser;
    }

    private void roleMenuHandle(SysUserVO sysUserVO) {
        if (null != sysUserVO.getRoleIds() && !sysUserVO.getRoleIds().isEmpty()) {
            List<SysUserRole> sysRoleMenus = new ArrayList<>();
            for (String roleId : sysUserVO.getRoleIds()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUserVO.getUserId());
                sysUserRole.setRoleId(roleId);
                sysRoleMenus.add(sysUserRole);
            }
            sysUserMapper.userRoleBath(sysRoleMenus);
        }
    }
    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    public SysUserVO update(SysUserVO sysUser){
        sysUserMapper.update(sysUser);
        roleMenuHandle(sysUser);
        sysUserMapper.delUserRole(sysUser.getUserId(), sysUser.getRoleIds());
        return sysUser;
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    public boolean deleteById(long userId){
        int total = sysUserMapper.deleteById(userId);
        return total > 0;
    }
}